package com.wx.mall.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wx.mall.common.exception.BusinessException;
import com.wx.mall.sys.entity.Role;
import com.wx.mall.sys.entity.RolePermission;
import com.wx.mall.sys.entity.UserRole;
import com.wx.mall.sys.mapper.RoleMapper;
import com.wx.mall.sys.mapper.RolePermissionMapper;
import com.wx.mall.sys.mapper.UserRoleMapper;
import com.wx.mall.sys.service.IRoleService;
import com.wx.mall.sys.vo.req.PageReqVO;
import com.wx.mall.sys.vo.req.RoleSetPermissionReqVO;
import com.wx.mall.sys.vo.resp.PageVO;
import com.wx.mall.sys.vo.resp.RoleFindPageRespVO;
import com.wx.mall.sys.vo.resp.UserPageRespVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author j
 * @since 2020-03-18
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public PageVO findPageAll(PageReqVO vo) {
        Page<Object> objectPage = PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        List<RoleFindPageRespVO> pageAll = roleMapper.findPageAll(vo);
        PageVO<RoleFindPageRespVO> pageVO = new PageVO<>();
        pageVO.setList(pageAll);
        pageVO.setTotalRows(objectPage.getTotal());
        System.out.println("总数量" + objectPage.getTotal());
        System.out.println("数据:" + pageAll);
        return pageVO;
    }

    @Override
    public void deleteById(Integer id) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", id);
        List<UserRole> userRoles = userRoleMapper.selectList(queryWrapper);
        if (userRoles.size() != 0) {
            throw new BusinessException(100003, "删除失败，用户拥有此角色");
        }
        int i = roleMapper.deleteById(id);
        if (i == 0) {
            throw new BusinessException(100004, "删除失败");
        }
    }

    @Override
    public void setPermission(RoleSetPermissionReqVO vo) {
        //先判断该角色是否又权限
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", vo.getRoleId());
        List<RolePermission> rolePermissions = rolePermissionMapper.selectList(queryWrapper);
        if (rolePermissions.size() == 0) {
            //新增权限
            setPermissionAdd(vo);
        } else {
            int delete = rolePermissionMapper.delete(queryWrapper);
            if (delete == 0) {
                throw new BusinessException(200001, "设置权限失败");
            }
            //新增权限
            setPermissionAdd(vo);
        }
    }

    //新增权限
    public void setPermissionAdd(RoleSetPermissionReqVO vo) {
        Integer adds = rolePermissionMapper.adds(vo.getRoleId(), vo.getPermissions());
    }
}
